package com.simon.hse.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.simon.hse.R;
import com.simon.hse.http.CommonCallBack;
import com.simon.hse.http.HttpManager;
import com.simon.hse.http.entity.LawOutput;
import com.simon.hse.http.entity.LoginInput;
import com.simon.hse.http.entity.LoginOutput;
import com.simon.hse.utils.PreferencesUtils;
import com.simon.hse.utils.ToastUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.Timer;
import java.util.TimerTask;

import retrofit2.Call;

/**
 * 登录a
 */
@SuppressWarnings("all")
@ContentView(R.layout.activity_login)
public class LoginActivity extends BaseActivity {
    @ViewInject(R.id.et_phone)
    EditText mEt_phone;

    @ViewInject(R.id.et_password)
    EditText mEt_password;

    @ViewInject(R.id.iv_eye)
    ImageView mIv_eye;

    private boolean isExit = false;// 是否退出
    private boolean isShowPsd = false;// 是否显示密码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Event(value = {R.id.tv_login, R.id.iv_eye, R.id.tv_forget})
    private void clickButton(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
                login();
                break;
            case R.id.iv_eye:
                if (!isShowPsd) {// 密码可见
                    isShowPsd = true;
                    mIv_eye.setImageResource(R.mipmap.eye_icon);
                    mEt_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {// 密码不可见
                    isShowPsd = false;
                    mIv_eye.setImageResource(R.mipmap.eye_icon_close);
                    mEt_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.tv_forget:
                startActivity(new Intent(LoginActivity.this, ForgetPsdActivity.class));
                break;
        }
    }

    /**
     * 登录
     */
    private void login() {
        final String phone = mEt_phone.getText().toString();
        final String password = mEt_password.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.getInstance().showShortToast("手机号不可为空!");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            ToastUtils.getInstance().showShortToast("密码不可为空!");
            return;
        }

        LoginInput loginInput = new LoginInput();
        loginInput.setLoginName(phone);
        loginInput.setPassword(password);


        Call<String> call = HttpManager.getInstance().requestService().LOGIN(HttpManager.getInstance().requestBody(loginInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                LoginOutput loginOutput = new Gson().fromJson(result, LoginOutput.class);
                if (loginOutput.getResult().equals("Success")) {
                    PreferencesUtils.putBoolean("LOGIN_STATE", true);
                    PreferencesUtils.putString("USERNAME", phone);
                    PreferencesUtils.putString("PASSWORD", password);

                    ToastUtils.getInstance().showShortToastBottom("登录成功!");
                    startActivity(new Intent(LoginActivity.this, HomeActivity.class));
                    finishActivity(LoginActivity.this);
                } else {
                    ToastUtils.getInstance().showShortToastBottom(loginOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });

    }

    /*双击退出*/
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();// 这里也可以弹出对话框
        }
        return false;
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            ToastUtils.getInstance().showShortToastBottom("再按一次退出程序");
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false;
                }
            }, 2000);
        } else {
            // 清空activity
            XActivityStack.getInstance().finishAllActivity();
        }
    }
}
